package com.atguigu.yygh.hosp.controller.api;

import com.alibaba.excel.util.StringUtils;
import com.alibaba.fastjson.JSONObject;
import com.atguigu.yygh.common.exception.YyghException;
import com.atguigu.yygh.common.helper.HttpRequestHelper;
import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.SignUtil;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import com.atguigu.yygh.vo.hosp.DepartmentQueryVo;
import com.atguigu.yygh.vo.hosp.ScheduleQueryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author ybl
 * @version 1.9
 * @date 2021/5/17 9:17
 */
@Api(tags = "医院管理API接口")
@RestController
@RequestMapping("api/hosp")
public class ApiController {

  @Autowired
  private HospitalService hospitalService;

  @Autowired
  private HospitalSetService hospitalSetService;

  @Autowired
  private DepartmentService departmentService;

  @Autowired
  private ScheduleService scheduleService;

  // 删除排班信息
  @PostMapping("schedule/remove")
  public Result removeSchedule(HttpServletRequest request){
    // 获取请求信息
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 医院编号
    String hoscode = (String)parameterMap.get("hoscode");
    // 科室编号
    String hosScheduleId = (String)parameterMap.get("hosScheduleId");

    // 必要的参数验证 签名验证
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);

    scheduleService.remove(hoscode,hosScheduleId);

    return Result.ok();
  }

  // 分页查询排班信息
  @PostMapping("schedule/list")
  public Result findScheduleByPage(HttpServletRequest request){
    // 获取请求信息
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());

    // 医院编号
    String hoscode = (String)parameterMap.get("hoscode");

    // 当前页数
    int page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 : Integer.parseInt(parameterMap.get("page").toString());
    // 每页显示行数
    int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 : Integer.parseInt(parameterMap.get("limit").toString());

    //必须参数校验 略
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);


    ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
    scheduleQueryVo.setHoscode(hoscode);
    Page<Schedule> pageModule = scheduleService.findPageDepartment(page,limit,scheduleQueryVo);

    return Result.ok(pageModule);
  }

  // 添加排班信息
  @PostMapping("saveSchedule")
  public Result saveSchedule(HttpServletRequest request){
    // 获取参数
    Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());

    // 医院编号
    String hoscode = (String) paramMap.get("hoscode");

    // 验证签名 hoscode
    SignUtil.signVerify(hoscode,hospitalSetService,paramMap);

    // 保存方法
    scheduleService.save(paramMap);
    return Result.ok();
  }

  // 删除科室
  @PostMapping("department/remove")
  public Result removeDepartment(HttpServletRequest request){
    // 获取请求信息
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 医院编号
    String hoscode = (String)parameterMap.get("hoscode");
    // 科室编号
    String depcode = (String)parameterMap.get("depcode");

    // 必要的参数验证 签名验证
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);

    departmentService.remove(hoscode,depcode);

    return Result.ok();
  }

  // 查询科室
  @PostMapping("department/list")
  public Result findDepartment(HttpServletRequest request){
// 获取请求信息
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());

    // 医院编号
    String hoscode = (String)parameterMap.get("hoscode");

    // 当前页数
    int page = StringUtils.isEmpty(parameterMap.get("page")) ? 1 : Integer.parseInt(parameterMap.get("page").toString());
    // 每页显示行数
    int limit = StringUtils.isEmpty(parameterMap.get("limit")) ? 10 : Integer.parseInt(parameterMap.get("limit").toString());

    //必须参数校验 略
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);


    DepartmentQueryVo departmentQueryVo = new DepartmentQueryVo();
    departmentQueryVo.setHoscode(hoscode);
    Page<Department> pageModule = departmentService.findPageDepartment(page,limit,departmentQueryVo);

    return Result.ok(pageModule);
  }

  // 上传科室
  @PostMapping("saveDepartment")
  public Result saveDepartment(HttpServletRequest request){
    // 获取请求信息
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());

    String hoscode = (String)parameterMap.get("hoscode");

    //必须参数校验 略
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);

    // 添加科室
    departmentService.save(parameterMap);
    return Result.ok();
  }

  // 从mongodb中查询医院信息
  @PostMapping("hospital/show")
  public Result getHospital(HttpServletRequest request){
    // 获取请求参数并封装
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
    //必须参数校验 略
    String hoscode = (String)parameterMap.get("hoscode");

    // //必须参数的校验 略 签名校验
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);

    return Result.ok(hospitalService.getByHoscode(hoscode));
  }

  @ApiOperation("上传医院")
  @PostMapping("saveHospital")
  public Result saveHospital(HttpServletRequest request){
    // 获取请求参数并转化格式
    Map<String, Object> parameterMap = HttpRequestHelper.switchMap(request.getParameterMap());
    // 获取hoscode
    String hoscode = (String) parameterMap.get("hoscode");

    // //必须参数的校验 略 签名校验
    SignUtil.signVerify(hoscode,hospitalSetService,parameterMap);


    //传输过程中“+”转换为了“ ”，因此我们要转换回来
    String logoDataString = (String)parameterMap.get("logoData");
    if(!StringUtils.isEmpty(logoDataString)) {
      String logoData = logoDataString.replaceAll(" ", "+");
      parameterMap.put("logoData", logoData);
    }

    // 调用添加方法
    hospitalService.save(parameterMap);
    return Result.ok();
  }
}
